﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using VersionComparer.DataModel.Actions;
using System.Drawing;

namespace VersionComparer.DataModel
{
    public abstract class Action
    {
        #region statics
        protected static Image InitializeImage(Image image)
        {
            Bitmap bmp = image as Bitmap;
            if (bmp == null)
                return image;
            bmp.MakeTransparent(Color.White);
            return bmp;
        }
        #endregion statics
        
        #region fields
        bool refreshAfterFinish;
        #endregion fields
        
        #region constrcutors

        public Action()
        {
        }
        #endregion constrcutors

        #region properties
        [DisplayName("Shortcut key")]
        [Description("Defines a shortcut key for this action. \r\nIf none is specified, a shortcut key will be automatically assigned.")]
        public virtual Keys ShortcutKey
        {
            get;
            set;
        }

        [DisplayName ("Action name")]
        [Description("Name to identify the action")]
        public string Name
        {
            get;
            set;
        }
        [Browsable (false)]
        public virtual string DisplayName
        {
            get
            {
                return Name;
            }
            set
            {
                Name = value;
            }
        
        
        }

        /// <summary>
        /// Flag indicating if after executing this action the list of results should be refreshed
        /// </summary>
        [DisplayName("Refresh results after action execution")]
        [Description ("If true then the results lists will be refreshed after the execution of this action")]
        public virtual bool RefreshAfterFinish
        {
            get
            {
                return refreshAfterFinish;
            }
            set
            {
                refreshAfterFinish = value;
            }
        }
        /// <summary>
        /// Action parameters
        /// </summary>
        [DisplayName ("Action parameters")]
        [Description ( "Defines the action parameters. Possible values are None (no parameters), SelectedRootItems (the name of the selected root items is passed) or SelectedItems (the names of the selected items is passed)")]
        public ActionParameters Parameters
        {
            get;
            set;
        }

        #endregion properties

        public override string ToString()
        {
            return Name;
        }


        protected abstract void ExecuteAction(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems);
        public virtual bool MayBeExecuted(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            return true;
        }
        public void Execute(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            if (focusedSelectedItems == null)
                focusedSelectedItems = new ComparableItem[0];
            if (unfocusedSelectedItems == null)
                unfocusedSelectedItems = new ComparableItem[0];
            if (!MayBeExecuted(mainForm, focusedSelectedItems, unfocusedSelectedItems))
                return;

            List<ComparableItem> parameters = new List<ComparableItem>();
            switch (Parameters)
            {
                case ActionParameters.SelectedRootItems:
                    {
                        foreach (ComparableItem item in focusedSelectedItems)
                            if (!parameters.Contains(item.Root))
                                parameters.Add(item.Root);

                        foreach (ComparableItem item in unfocusedSelectedItems)
                            if (!parameters.Contains(item.Root))
                                parameters.Add(item.Root);
                    }
                    break;
                case ActionParameters.SelectedItems:
                    parameters = new List<ComparableItem>(focusedSelectedItems);
                    break;
            }
            ExecuteAction(mainForm, focusedSelectedItems, unfocusedSelectedItems);
        }
        public virtual Image Image(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            return null;
        }
    }
}
